package com.esri.core.internal.widget;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Scroller;

/* loaded from: classes.dex */
public class InfiniteGallery extends AdapterView<Adapter> implements GestureDetector.OnGestureListener {
    private static final int G = 250;
    private float A;
    private float B;
    private boolean C;
    private d D;
    private boolean E;
    private boolean F;
    private Runnable H;
    int a;
    boolean b;
    int c;
    int d;
    int e;
    int f;
    int g;
    boolean h;
    boolean i;
    private b j;
    private GestureDetector k;
    private Adapter l;
    private int m;
    private int n;
    private int o;
    private Rect p;
    private c q;
    private View r;
    private short s;
    private boolean t;
    private View u;
    private a v;
    private int w;
    private int x;
    private boolean y;
    private int z;

    private int e(int i) {
        return i;
    }

    @Override // android.view.GestureDetector.OnGestureListener
    public void onLongPress(MotionEvent motionEvent) {
    }

    @Override // android.view.GestureDetector.OnGestureListener
    public void onShowPress(MotionEvent motionEvent) {
    }

    public InfiniteGallery(Context context) {
        super(context);
        this.a = 200;
        this.j = new b();
        this.k = null;
        this.q = new c();
        this.s = (short) 0;
        this.t = true;
        this.b = false;
        this.c = 0;
        this.d = 0;
        this.e = 0;
        this.f = -1;
        this.y = false;
        this.z = 20;
        this.h = true;
        this.C = false;
        this.E = true;
        this.H = new Runnable() { // from class: com.esri.core.internal.widget.InfiniteGallery.1
            @Override // java.lang.Runnable
            public void run() {
                InfiniteGallery.this.i = false;
                InfiniteGallery.this.g();
            }
        };
        setFocusable(true);
        setWillNotDraw(false);
        this.y = true;
        a(context);
    }

    public InfiniteGallery(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.a = 200;
        this.j = new b();
        this.k = null;
        this.q = new c();
        this.s = (short) 0;
        this.t = true;
        this.b = false;
        this.c = 0;
        this.d = 0;
        this.e = 0;
        this.f = -1;
        this.y = false;
        this.z = 20;
        this.h = true;
        this.C = false;
        this.E = true;
        this.H = new Runnable() { // from class: com.esri.core.internal.widget.InfiniteGallery.1
            @Override // java.lang.Runnable
            public void run() {
                InfiniteGallery.this.i = false;
                InfiniteGallery.this.g();
            }
        };
        a(context);
    }

    public InfiniteGallery(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.a = 200;
        this.j = new b();
        this.k = null;
        this.q = new c();
        this.s = (short) 0;
        this.t = true;
        this.b = false;
        this.c = 0;
        this.d = 0;
        this.e = 0;
        this.f = -1;
        this.y = false;
        this.z = 20;
        this.h = true;
        this.C = false;
        this.E = true;
        this.H = new Runnable() { // from class: com.esri.core.internal.widget.InfiniteGallery.1
            @Override // java.lang.Runnable
            public void run() {
                InfiniteGallery.this.i = false;
                InfiniteGallery.this.g();
            }
        };
        a(context);
    }

    private void a(Context context) {
        GestureDetector gestureDetector = new GestureDetector(context, this);
        this.k = gestureDetector;
        gestureDetector.setIsLongpressEnabled(true);
    }

    public void setCallbackOnUnselectedItemClick(boolean z) {
        this.t = z;
    }

    @Override // android.view.GestureDetector.OnGestureListener
    public boolean onDown(MotionEvent motionEvent) {
        this.j.a(false);
        this.s = (short) 0;
        int a2 = a((int) motionEvent.getX(), (int) motionEvent.getY());
        this.g = a2;
        if (a2 >= 0) {
            View childAt = getChildAt(a2 - this.e);
            this.u = childAt;
            childAt.setPressed(true);
        }
        this.F = true;
        return true;
    }

    void a() {
        if (this.j.a.isFinished()) {
            c();
        }
    }

    void b() {
        a();
    }

    @Override // android.view.GestureDetector.OnGestureListener
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
        if (this.l.getCount() == 1) {
            return false;
        }
        if (!this.E) {
            removeCallbacks(this.H);
            if (!this.i) {
                this.i = true;
            }
        }
        if (f < 0.0f) {
            this.s = (short) 1;
        } else if (f == 0.0f) {
            this.s = (short) 0;
        } else {
            this.s = (short) -1;
        }
        a(((int) f) / 2);
        return true;
    }

    protected void a(float f) {
        this.j.a((int) (-f));
    }

    @Override // android.view.GestureDetector.OnGestureListener
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
        if (this.l.getCount() == 1) {
            return false;
        }
        getParent().requestDisallowInterceptTouchEvent(true);
        if (this.E) {
            if (this.F) {
                if (!this.i) {
                    this.i = true;
                }
                postDelayed(this.H, 250L);
            }
        } else if (this.i) {
            this.i = false;
        }
        b(((int) f) * (-1));
        this.F = false;
        return true;
    }

    @Override // android.widget.AdapterView
    public View getSelectedView() {
        int i;
        if (this.d <= 0 || (i = this.f) < 0) {
            return null;
        }
        return getChildAt(i - this.e);
    }

    public int a(int i, int i2) {
        Rect rect = this.p;
        if (rect == null) {
            rect = new Rect();
            this.p = rect;
        }
        for (int childCount = getChildCount() - 1; childCount >= 0; childCount--) {
            View childAt = getChildAt(childCount);
            if (childAt.getVisibility() == 0) {
                childAt.getHitRect(rect);
                if (rect.contains(i, i2)) {
                    return this.e + childCount;
                }
            }
        }
        return -1;
    }

    void c() {
        View view;
        if (getChildCount() == 0 || (view = this.r) == null) {
            return;
        }
        int j = j() - a(view);
        if (j != 0) {
            this.j.b(j);
        } else {
            n();
        }
    }

    void a(int i) {
        this.f = i;
        i();
    }

    private void i() {
        View view = this.r;
        View childAt = getChildAt(this.f - this.e);
        this.r = childAt;
        if (childAt == null) {
            return;
        }
        childAt.setSelected(true);
        childAt.setFocusable(true);
        if (hasFocus()) {
            childAt.requestFocus();
        }
        if (view != null) {
            view.setSelected(false);
            view.setFocusable(false);
        }
        if (this.f == this.d) {
            this.f = 0;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:32:0x0061  */
    /* JADX WARN: Removed duplicated region for block: B:38:? A[RETURN, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    private void a(boolean r6) {
        /*
            r5 = this;
            android.view.View r0 = r5.r
            if (r0 != 0) goto L5
            return
        L5:
            int r1 = r5.j()
            int r2 = r1 / 3
            if (r0 == 0) goto L1d
            int r3 = r0.getLeft()
            if (r3 > r2) goto L1d
            int r0 = r0.getRight()
            int r3 = r2 * 2
            int r3 = r3 + r1
            if (r0 < r3) goto L1d
            return
        L1d:
            short r0 = r5.s
            r3 = 0
            r4 = 1
            if (r0 == r4) goto L42
            if (r0 != 0) goto L28
            if (r6 == 0) goto L28
            goto L42
        L28:
            r6 = 0
        L29:
            int r0 = r5.getChildCount()
            if (r6 <= r0) goto L5a
            android.view.View r0 = r5.getChildAt(r6)
            int r0 = r0.getRight()
            int r4 = r2 * 2
            int r4 = r1 - r4
            if (r0 < r4) goto L3f
        L3d:
            r3 = r6
            goto L5a
        L3f:
            int r6 = r6 + 1
            goto L29
        L42:
            int r6 = r5.getChildCount()
            int r6 = r6 - r4
        L47:
            if (r6 < 0) goto L5a
            android.view.View r0 = r5.getChildAt(r6)
            int r0 = r0.getLeft()
            int r4 = r2 * 2
            int r4 = r4 + r1
            if (r0 > r4) goto L57
            goto L3d
        L57:
            int r6 = r6 + (-1)
            goto L47
        L5a:
            int r6 = r5.e
            int r6 = r6 + r3
            int r0 = r5.f
            if (r6 == r0) goto L64
            r5.a(r6)
        L64:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.esri.core.internal.widget.InfiniteGallery.a(boolean):void");
    }

    public float d() {
        return this.A;
    }

    public float e() {
        return this.B;
    }

    private boolean c(int i) {
        View childAt = getChildAt(i);
        if (childAt != null) {
            this.j.b(j() - a(childAt));
            return true;
        }
        return false;
    }

    @Override // android.view.GestureDetector.OnGestureListener
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        int i = this.g;
        if (i >= 0) {
            c(i - this.e);
            if (this.t || this.g == this.f) {
                this.A = motionEvent.getX();
                this.B = motionEvent.getY();
                View view = this.u;
                int i2 = this.g;
                performItemClick(view, i2, this.l.getItemId(i2));
                return true;
            }
            return true;
        }
        return false;
    }

    void b(int i) {
        if (getChildCount() == 0) {
            return;
        }
        boolean z = i < 0;
        int e = e(i);
        if (e != i) {
            this.j.b(false);
            n();
        }
        d(e);
        b(z);
        if (z) {
            m();
        } else {
            c(true);
        }
        this.q.a();
        a(z);
        invalidate();
    }

    private void b(boolean z) {
        int i;
        int i2;
        int childCount = getChildCount();
        int i3 = this.e;
        if (z) {
            i = 0;
            for (int i4 = 0; i4 < childCount; i4++) {
                View childAt = getChildAt(i4);
                if (childAt.getRight() >= 0) {
                    break;
                }
                i++;
                this.q.a(i3 + i4, childAt);
            }
            i2 = 0;
        } else {
            int width = getWidth() - 0;
            i = 0;
            i2 = 0;
            for (int i5 = childCount - 1; i5 >= 0; i5--) {
                View childAt2 = getChildAt(i5);
                if (childAt2.getLeft() <= width) {
                    break;
                }
                i++;
                this.q.a(i3 + i5, childAt2);
                i2 = i5;
            }
        }
        detachViewsFromParent(i2, i);
        if (z) {
            int i6 = this.e + i;
            this.e = i6;
            if (i6 == this.d) {
                this.e = 0;
            }
        }
    }

    private int j() {
        return (((getWidth() - getPaddingLeft()) - getPaddingRight()) / 2) + getPaddingLeft();
    }

    private static int a(View view) {
        return view.getLeft() + (view.getWidth() / 2);
    }

    private void d(int i) {
        for (int childCount = getChildCount() - 1; childCount >= 0; childCount--) {
            getChildAt(childCount).offsetLeftAndRight(i);
        }
    }

    @Override // android.view.View
    public boolean onTouchEvent(MotionEvent motionEvent) {
        boolean onTouchEvent = this.k.onTouchEvent(motionEvent);
        int action = motionEvent.getAction();
        if (action == 1) {
            a();
        } else if (action == 3) {
            b();
        }
        return onTouchEvent;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public class b implements Runnable {
        Scroller a;
        private int c;

        public b() {
            this.a = new Scroller(InfiniteGallery.this.getContext());
        }

        private void a() {
            InfiniteGallery.this.removeCallbacks(this);
        }

        public void a(int i) {
            if (i == 0) {
                return;
            }
            a();
            int i2 = i < 0 ? Integer.MAX_VALUE : 0;
            this.c = i2;
            this.a.fling(i2, 0, i, 0, 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE);
            InfiniteGallery.this.post(this);
        }

        public void b(int i) {
            if (i == 0) {
                return;
            }
            a();
            this.c = 0;
            this.a.startScroll(0, 0, -i, 0, InfiniteGallery.this.a);
            InfiniteGallery.this.post(this);
        }

        public void a(boolean z) {
            InfiniteGallery.this.removeCallbacks(this);
            b(z);
        }

        void b(boolean z) {
            this.a.forceFinished(true);
            if (z) {
                InfiniteGallery.this.c();
            }
        }

        @Override // java.lang.Runnable
        public void run() {
            int max;
            InfiniteGallery.this.b = false;
            Scroller scroller = this.a;
            boolean computeScrollOffset = scroller.computeScrollOffset();
            int currX = scroller.getCurrX();
            int i = this.c - currX;
            if (i > 0) {
                InfiniteGallery infiniteGallery = InfiniteGallery.this;
                infiniteGallery.g = infiniteGallery.e;
                max = Math.min(((InfiniteGallery.this.getWidth() - InfiniteGallery.this.getPaddingLeft()) - InfiniteGallery.this.getPaddingRight()) - 1, i);
            } else {
                InfiniteGallery infiniteGallery2 = InfiniteGallery.this;
                infiniteGallery2.g = infiniteGallery2.e + (InfiniteGallery.this.getChildCount() - 1);
                max = Math.max(-(((InfiniteGallery.this.getWidth() - InfiniteGallery.this.getPaddingRight()) - InfiniteGallery.this.getPaddingLeft()) - 1), i);
            }
            InfiniteGallery.this.b(max);
            if (computeScrollOffset && !InfiniteGallery.this.b) {
                this.c = currX;
                InfiniteGallery.this.post(this);
                return;
            }
            b(true);
        }
    }

    /* loaded from: classes.dex */
    class a extends DataSetObserver {
        private Parcelable b = null;

        a() {
        }

        @Override // android.database.DataSetObserver
        public void onChanged() {
            InfiniteGallery.this.h = true;
            InfiniteGallery infiniteGallery = InfiniteGallery.this;
            infiniteGallery.c = infiniteGallery.d;
            InfiniteGallery infiniteGallery2 = InfiniteGallery.this;
            infiniteGallery2.d = infiniteGallery2.getAdapter().getCount();
            InfiniteGallery.this.a(InfiniteGallery.this.d > 0 ? 0 : -1);
            if (InfiniteGallery.this.getAdapter().hasStableIds() && this.b != null && InfiniteGallery.this.c == 0 && InfiniteGallery.this.d > 0) {
                InfiniteGallery.this.onRestoreInstanceState(this.b);
                this.b = null;
            }
            InfiniteGallery.this.requestLayout();
        }

        @Override // android.database.DataSetObserver
        public void onInvalidated() {
            InfiniteGallery.this.h = true;
            if (InfiniteGallery.this.getAdapter().hasStableIds()) {
                this.b = InfiniteGallery.this.onSaveInstanceState();
            }
            InfiniteGallery infiniteGallery = InfiniteGallery.this;
            infiniteGallery.c = infiniteGallery.d;
            InfiniteGallery.this.d = 0;
            InfiniteGallery.this.f = -1;
            InfiniteGallery.this.requestLayout();
        }

        public void a() {
            this.b = null;
        }
    }

    @Override // android.widget.AdapterView
    public Adapter getAdapter() {
        return this.l;
    }

    @Override // android.widget.AdapterView
    public void setAdapter(Adapter adapter) {
        Adapter adapter2 = this.l;
        if (adapter2 != null) {
            adapter2.unregisterDataSetObserver(this.v);
            k();
        }
        this.l = adapter;
        if (adapter != null) {
            this.c = this.d;
            this.d = adapter.getCount();
            a aVar = new a();
            this.v = aVar;
            this.l.registerDataSetObserver(aVar);
            a(this.d > 0 ? 0 : -1);
        } else {
            k();
        }
        requestLayout();
    }

    @Override // android.widget.AdapterView
    public void setSelection(int i) {
        a(i);
    }

    @Override // android.widget.AdapterView, android.view.ViewGroup, android.view.View
    protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
        super.onLayout(z, i, i2, i3, i4);
        if (this.l == null) {
            return;
        }
        this.C = true;
        f();
        this.C = false;
    }

    private void k() {
        this.h = false;
        removeAllViewsInLayout();
        a(-1);
        invalidate();
    }

    void f() {
        int right = getRight() - getLeft();
        if (this.d == 0) {
            k();
            return;
        }
        if (getSelectedItemPosition() >= 0) {
            a(getSelectedItemPosition());
        }
        if (this.f == this.d) {
            this.f = 0;
        }
        l();
        detachAllViewsFromParent();
        this.o = 0;
        this.n = 0;
        int i = this.f;
        this.e = i;
        View a2 = a(i, 0, true);
        a2.offsetLeftAndRight(((right / 2) + 0) - (a2.getWidth() / 2));
        m();
        c(false);
        this.q.a();
        invalidate();
        this.h = false;
        i();
        g();
    }

    private void l() {
        int childCount = getChildCount();
        c cVar = this.q;
        for (int i = 0; i < childCount; i++) {
            cVar.a(this.e + i, getChildAt(i));
        }
    }

    @Override // android.view.View
    protected void onMeasure(int i, int i2) {
        int childCount = getChildCount();
        int i3 = 0;
        int i4 = 0;
        for (int i5 = 0; i5 < childCount; i5++) {
            View childAt = getChildAt(i5);
            if (this.y || childAt.getVisibility() != 8) {
                measureChildWithMargins(childAt, i, 0, i2, 0);
                i3 = Math.max(i3, childAt.getMeasuredWidth());
                i4 = Math.max(i4, childAt.getMeasuredHeight());
            }
        }
        setMeasuredDimension(resolveSize(i3, i), resolveSize(i4, i2));
        this.w = i;
        this.x = i2;
    }

    private View a(int i, int i2, boolean z) {
        View a2;
        if (!this.h && (a2 = this.q.a(i)) != null) {
            int left = a2.getLeft();
            this.o = Math.max(this.o, a2.getMeasuredWidth() + left);
            this.n = Math.min(this.n, left);
            a(a2, i2, z);
            return a2;
        }
        View view = this.l.getView(i, null, this);
        a(view, i2, z);
        return view;
    }

    @Override // android.view.ViewGroup
    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new ViewGroup.LayoutParams(-2, -2);
    }

    private void a(View view, int i, boolean z) {
        int i2;
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams == null) {
            layoutParams = generateDefaultLayoutParams();
        }
        addViewInLayout(view, z ? -1 : 0, layoutParams);
        view.measure(ViewGroup.getChildMeasureSpec(this.w, 0, layoutParams.width), ViewGroup.getChildMeasureSpec(this.x, 0, layoutParams.height));
        int a2 = a(view, true);
        int measuredHeight = view.getMeasuredHeight() + a2;
        int measuredWidth = view.getMeasuredWidth();
        if (z) {
            i2 = measuredWidth + i;
        } else {
            int i3 = i - measuredWidth;
            i2 = i;
            i = i3;
        }
        view.layout(i, a2, i2, measuredHeight);
    }

    private int a(View view, boolean z) {
        int measuredHeight = z ? getMeasuredHeight() : getHeight();
        int measuredHeight2 = z ? view.getMeasuredHeight() : view.getHeight();
        int i = this.m;
        if (i != 16) {
            if (i == 48 || i != 80) {
                return 0;
            }
            return measuredHeight - measuredHeight2;
        }
        return (measuredHeight - measuredHeight2) / 2;
    }

    private void c(boolean z) {
        int right;
        int i;
        int i2 = this.z;
        View childAt = getChildAt(0);
        if (childAt != null) {
            i = this.e - 1;
            right = childAt.getLeft() - i2;
        } else {
            right = getRight() - getLeft();
            this.b = true;
            i = 0;
        }
        if (z && i == -1 && this.f == 0) {
            int childCount = getChildCount();
            int i3 = this.d;
            if (getChildAt(childCount - 1) != null) {
                this.e = i3;
                i = i3 - 1;
                right = 0;
            }
        }
        while (right > 0 && i >= 0) {
            View a2 = a(i, right, false);
            this.e = i;
            right = a2.getLeft() - i2;
            i--;
        }
    }

    private void m() {
        int i;
        int i2 = this.z;
        int right = getRight() - getLeft();
        int childCount = getChildCount();
        int i3 = this.d;
        View childAt = getChildAt(childCount - 1);
        if (childAt != null) {
            int i4 = this.e;
            int i5 = i4 != this.d - 1 ? i4 + childCount : 0;
            r5 = childAt.getRight() + i2;
            i = i5;
        } else {
            i = this.d - 1;
            this.e = i;
            this.b = true;
        }
        while (r5 < right && i < i3) {
            r5 = a(i, r5, true).getRight() + i2;
            i++;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class c {
        private SparseArray<View> b = new SparseArray<>();

        c() {
        }

        public void a(int i, View view) {
            this.b.put(i, view);
        }

        View a(int i) {
            View view = this.b.get(i);
            if (view != null) {
                this.b.delete(i);
            }
            return view;
        }

        View b(int i) {
            return this.b.get(i);
        }

        void a() {
            SparseArray<View> sparseArray = this.b;
            int size = sparseArray.size();
            for (int i = 0; i < size; i++) {
                View valueAt = sparseArray.valueAt(i);
                if (valueAt != null) {
                    InfiniteGallery.this.removeDetachedView(valueAt, true);
                }
            }
            sparseArray.clear();
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public class d extends Handler implements Runnable {
        public d() {
        }

        @Override // java.lang.Runnable
        public void run() {
            if (InfiniteGallery.this.h) {
                post(this);
            } else {
                InfiniteGallery.this.h();
            }
        }
    }

    void g() {
        if (getOnItemSelectedListener() != null) {
            if (this.C) {
                if (this.D == null) {
                    this.D = new d();
                }
                d dVar = this.D;
                dVar.post(dVar);
                return;
            }
            h();
        }
    }

    void h() {
        if (getOnItemSelectedListener() == null) {
            return;
        }
        int i = this.f;
        if (i >= 0) {
            getOnItemSelectedListener().onItemSelected(this, getSelectedView(), i, getAdapter().getItemId(i));
            return;
        }
        getOnItemSelectedListener().onNothingSelected(this);
    }

    private void n() {
        if (this.i) {
            this.i = false;
            g();
        }
    }

    @Override // android.view.View
    protected void onRestoreInstanceState(Parcelable parcelable) {
        super.onRestoreInstanceState(parcelable);
    }

    @Override // android.view.ViewGroup
    protected void removeDetachedView(View view, boolean z) {
        super.removeDetachedView(view, z);
    }

    @Override // android.view.View
    protected Parcelable onSaveInstanceState() {
        return super.onSaveInstanceState();
    }
}
